package com.ujoku.service.impl;


import com.labillusion.core.database.base.mybatis.dao.BaseDao;
import com.labillusion.core.database.base.mybatis.service.base.impl.BaseServiceImpl;
import com.labillusion.core.platform.exception.ResourceNotFoundException;
import com.labillusion.core.platform.web.rest.exception.InvalidRequestException;
import com.labillusion.core.util.StringUtils;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import com.ujoku.context.constants.StatusConstants;
import com.ujoku.dao.GoodsSpecDao;
import com.ujoku.domain.*;
import com.ujoku.request.AddToCartForm;
import com.ujoku.request.UpdateCartForm;
import com.ujoku.service.*;
import org.hamcrest.Matchers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static ch.lambdaj.Lambda.*;

/**
 * Created by Greg on 2014/10/17.
 */
@Service
public class GoodSpecServiceImpl extends BaseServiceImpl<GoodsSpec> implements GoodsSpecService<GoodsSpec> {

    @Autowired
    private GoodsSpecDao goodSpecDao;

    @Autowired
    private MyListItemService myListItemService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CartItemService cartItemService;

    @Autowired
    private CartService cartService;

    @Override
    protected BaseDao<GoodsSpec> getBaseDao() {
        return goodSpecDao;
    }

    @Override
    public GoodsSpec selectGoodsSpecById(int goodsId) {
        return goodSpecDao.selectByGoodsId(goodsId);
    }

    @Override
    public void checkInventory(AddToCartForm form, Goods goods) {

        if(goods.getStock() == 0 || form.getQuantity() > goods.getStock())
            throw new InvalidRequestException("outOfStock",goods.getGoods_name() + "库存不足. 可用库存为"+ goods.getStock());

        if(StringUtils.hasText(form.getMyListItemId()))
        {
            MyListItem myListItem = (MyListItem) myListItemService.selectById(form.getMyListItemId());
            if(form.getQuantity() > myListItem.getQty()){
                throw new InvalidRequestException("outOfStock",myListItem.getGoodsName() + "库存不足. 可用库存为"+ myListItem.getQty());
            }
        }
    }

    @Override
    public void checkInventory(Integer inputQty, CartItem cartItem) {
        Goods goods = goodsService.selectById(cartItem.getGoodsId());
        if(inputQty > goods.getStock())
            throw new InvalidRequestException("outOfStock",goods.getGoods_name() + "库存不足. 可用库存为"+ goods.getStock());

        if(StringUtils.hasText(cartItem.getMyListItemId()))
        {
            MyListItem myListItem = (MyListItem) myListItemService.selectById(cartItem.getMyListItemId());
            if(inputQty > myListItem.getQty()){
                throw new InvalidRequestException("outOfStock",myListItem.getGoodsName() + "库存不足. 可用库存为"+ myListItem.getQty());
            }
        }
    }

    @Override
    public void checkInventory(CartItem cartItem) {
        Goods goods = goodsService.selectById(cartItem.getGoodsId());
        if(cartItem.getQty() > goods.getStock())
            throw new InvalidRequestException("outOfStock",goods.getGoods_name() + "库存不足. 可用库存为"+ goods.getStock());

        if(StringUtils.hasText(cartItem.getMyListItemId()))
        {
            MyListItem myListItem = (MyListItem) myListItemService.selectById(cartItem.getMyListItemId());
            if(cartItem.getQty() > myListItem.getQty()){
                throw new InvalidRequestException("outOfStock",myListItem.getGoodsName() + "库存不足. 可用库存为"+ myListItem.getQty()  );
            }
        }
    }

    @Override
    public boolean checkInventory(Cart cart, List<String> messageList){

        boolean reload = false;
        for(CartItem cartItem : cart.getCartItemList()){
            //私人酒窖
            if(StringUtils.hasText(cartItem.getMyListItemId()))
            {
                MyListItem myListItem = (MyListItem) myListItemService.selectById(cartItem.getMyListItemId());
                //产品下架
                if(!myListItem.getOnSale())
                {
                    cartItemService.deleteById(cartItem.getId());
                    messageList.add(String.format("由于产品下架, %s被移除.", cartItem.getGoodsName()));
                }
                //自己买自己的私人酒窖产品
                else if(myListItem.getMemberId() == cart.getMemberId())
                {
                    cartItemService.deleteById(cartItem.getId());
                    messageList.add(String.format("由于产品无效, %s被移除.", cartItem.getGoodsName()));
                }
                //库存为0
                else if(myListItem.getQty() == null || myListItem.getQty() <= 0) {
                    cartItemService.deleteById(cartItem.getId());
                    messageList.add(String.format("由于库存不足, %s被移除.", cartItem.getGoodsName()));
                    reload = true;
                }
                //库存不足
                else if(myListItem.getQty() < cartItem.getQty()){
                    cartItem.setQty(myListItem.getQty());
                    cartItemService.update(cartItem);

                    messageList.add(String.format("由于库存不足, %s数量被更新为%s.", cartItem.getGoodsName(), cartItem.getQty()));
                    reload = true;
                }

                return reload;
            }

            //普通商品
            Goods goods = goodsService.selectById(cartItem.getGoodsId());

            if(goods.getStock() == 0){
                cartItemService.deleteById(cartItem.getId());

                messageList.add(String.format("由于库存不足, %s被移除.", cartItem.getGoodsName()));
                reload = true;
            }
            else if(goods.getStock() < cartItem.getQty()){
                cartItem.setQty(goods.getStock());
                cartItemService.update(cartItem);

                messageList.add(String.format("由于库存不足, %s数量被更新为%s.", cartItem.getGoodsName(), cartItem.getQty()));
                reload = true;
            }

        }

        return reload;
    }

}
